//设计时候回显用
function initGlobleConditionsGrid(){
	if(Ext.getCmp('conditions_grid_') == null){
		//查询条件维护表格
		var sm = new Ext.grid.CheckboxSelectionModel();
		//加载grid的查询条件-------处理成这种格式,在原来值的基础上末尾加or
		//-----------and $SANZI_YB_A_[year].A11 > 400 or $SANZI_YB_A_[year].A11 <349 or
		
		var con = jQuery("#c_query_conditions").val();
		if(con!="" && typeof(con) != 'undefined')
		{
			//con += " and $SANZI_YB_A_[year].A11 > 400 or $SANZI_YB_A_[year].A11 <349";
			con += " or";
			var reg = /(and|or) .*?(?=and|or)/ig;  //正则-----截取and或者or
			var match = con.match(reg);
			var recordData = [];
			for(var i=0;i<match.length;i++)
			{
				if(match[i].indexOf("and")>=0)
				{
					var join = match[i].substring(match[i].indexOf("and"),match[i].indexOf("and")+3);
					var code = match[i].substring(match[i].indexOf("and")+3);
				}else
				{
					var join = match[i].substring(match[i].indexOf("or"),match[i].indexOf("or")+2);
					var code = match[i].substring(match[i].indexOf("or")+2);
				}
				var row = [];
				row.push(join);
				row.push(code);
				recordData.push(row);
			}
		}
		var conditions_grid = new Ext.grid.GridPanel({
			id:'conditions_grid_',
			title:'已创建的查询条件',
			region:'east',
			split:true,
			margins: '2 2 0 2',
			collapsible: true,
			width:300,
			store: new Ext.data.Store({
				autoLoad:true,
				data:[],
				reader: new Ext.data.ArrayReader({}, [{name: 'join'},{name: 'code'}])
			}),
			cm: new Ext.grid.ColumnModel([sm,
			                              {id:'a',header: "连接符",dataIndex: 'join'},{id:'b',header:"列条件",dataIndex:'code'}
			                              ]),
			                              sm:sm,
			                              stripeRows: true,
			                              autoExpandColumn: 'b',
			                              listeners:{rowclick : function(o,index,e){
			Ext.getCmp("selColumnConditionMethod_simple_conditions_").setValue(o.getStore().getAt(index).data.join);
			Ext.getCmp("selColumnCondition_simple_conditions_").setValue(o.getStore().getAt(index).data.code);
			Ext.getCmp("conditions_button_update").setDisabled(false);
			Ext.getCmp("conditions_button_add").setDisabled(true);
		},destory:function(){
			return false;
		}},
		tbar:[{
			text:'删除条件',
			iconCls:'remove',
			handler:function(){
			var grid=Ext.getCmp('conditions_grid_');
			records=grid.getSelectionModel().getSelections();
			for(var i=0;i<records.length;i++){
				grid.getStore().remove(records[i]);
			}
		}
		}]
		});
		//加载grid的数据
		if(con!="" && typeof(con) != 'undefined')
			conditions_grid.getStore().loadData(recordData);
	}
}
//主栏添加条件
function globleConditionsWin(){
	if(Ext.getCmp('customH_win_conditions_') == null){
		var advancedColumnWindow_conditions_ = new Ext.Window({	
			width:900,	
			height: 500,
			layout: "border",
			title:'添加自定义查询条件',
			evObject:null,
			modal: true,
			border:false,
			id: "customH_win_conditions_",
			closeAction: 'hide',
			listeners:{show:function(a){
			var customDropTarget2 = new Ext.dd.DropTarget(Ext.getCmp('selColumnCondition_simple_div_conditions_').getEl().dom, {
				ddGroup: 'customDDGroup',
				copy: false,
				notifyDrop: function(ddSource, e, data){
				node = ddSource.dragData.node;
				var a=Ext.getCmp('selColumnCondition_simple_conditions_');
				if(node.attributes.d == 'h'){
					a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.column);
				}else if(node.attributes.d == 'v'){
					if(node.attributes.val.indexOf("[tree]") == -1){
						a.setValue(a.getValue()+'$'+node.attributes.table+'.'+node.attributes.column + " = '" + node.attributes.val + "'");
					}else{
						a.setValue(a.getValue()+'$'+node.attributes.tableName+'.'+node.attributes.column + " like '" + node.attributes.pid + "%'");
					}
				}
				return(true);
			}
			});
		},hide:function(){
			Ext.getCmp('selColumnCondition_simple_conditions_').setValue('');
		}},
		items: [
		        {
		        	height:608,
		        	width:200,
		        	autoScroll: true,
		        	collapsible: true,
		        	id: 'tree-panel-conditions',
		        	region: 'west',
		        	title: '指标分类',
		        	enableDrag:true,
		        	ddGroup: 'customDDGroup',
		        	animate:false,
		        	split: true,
		        	xtype: 'treepanel',
		        	margins: '2 2 2 2',
		        	useArrows:true,
		        	rootVisible : false,
		        	root: {text: '指标分类',draggable: false,id: '-1',isRoot:true},
		        	loader: new Ext.tree.TreeLoader({
		        		autoLoad:false,
		        		dataUrl: getRootPath()+'/detailReport.report?decorate=no&isAjax=true',
		        		listeners:{beforeload:function(treeLoader, node){
		        		if(node.attributes.isRoot==true){
		        			this.baseParams.method = "getRoot";
		        		}else{
		        			this.baseParams.method = "getChilds";
		        		}
		        		this.baseParams.xmlId = xml_id_;
		        		this.baseParams.tableName = node.attributes.table;
		        		this.baseParams.isRoot = node.attributes.isRoot;
		        		this.baseParams.pid = node.attributes.val;
		        		this.baseParams.level = node.attributes.level;
		        		this.baseParams.dir = node.attributes.d;
		        		this.baseParams.isMeasures = 'true';//只显示事实表节点
		        		this.baseParams.time = node.attributes.time;
		        		this.baseParams.currentTime = jQuery("#currentTime").val();
		        		this.baseParams.currentTimeWeiBiao = jQuery("#currentTimeWeiBiao").val();
		        		this.baseParams.isModify = 1;
		        	}}
		        	}),
		        	listeners:{
		        		beforeappend:function(tree,parent,node){
		        	}
		        	}
		        }
		        ,{
		        	region:'center',
		        	height:608,
		        	width:400, 
		        	margins: '2 2 0 2',
		        	title:'添加查询条件',
		        	buttons:[{
		        		text:'关闭',
		        		handler:function(){
		        		advancedColumnWindow_conditions_.hide();
		        	}
		        	}],
		        	buttonAlign:'right',
		        	defaults:{margins: '2 2 2 2'},
		        	bodyStyle:'padding:5px 5px 0',
		        	items:[{
		        		xtype:'fieldset',
		        		title: '添加查询条件',
		        		margins: '2 2 2 2',
		        		collapsible: true,
		        		layout:'form',
		        		frame:true,
		        		defaults:{labelWidth:60,border:false},
		        		items:[{
		        			id:'selColumnConditionMethod_simple_div_conditions_',
		        			layout:'form',
		        			items:[{
		        				id:'selColumnConditionMethod_simple_conditions_',
		        				xtype:'combo',
		        				store:new Ext.data.SimpleStore({
		        					fields: ['name', 'valtext'],
		        					data : [['and','and'],['or', 'or']]
		        				}),
		        				fieldLabel:'连接符',
		        				displayField:'name',
		        				mode: 'local',
		        				value:'and',
		        				editable:false,
		        				valueField: "valtext",
		        				triggerAction: "all",
		        				width:60
		        			}]
		        		},{
		        			id:'selColumnCondition_simple_div_conditions_',
		        			layout:'form',
		        			items:[{
		        				id:'selColumnCondition_simple_conditions_',
		        				xtype:'textfield',
		        				fieldLabel:'查询条件',
		        				width:240,
		        				allowBlank:false
		        			}]
		        		},{
		        			id:'conditions_button_add',
		        			xtype:'button',
		        			text:'添加查询条件',
		        			handler:function(){
		        			var left = Ext.getCmp('selColumnConditionMethod_simple_conditions_').getValue();
		        			if(!Ext.getCmp('selColumnCondition_simple_conditions_').validate()){
		        				return;
		        			}
		        			var right = Ext.getCmp('selColumnCondition_simple_conditions_').getValue();
		        			Ext.getCmp('conditions_grid_').getStore().add(new Ext.data.Record({join:left,code:right}));
		        			Ext.getCmp("selColumnConditionMethod_simple_conditions_").setValue('and');
		        			Ext.getCmp("selColumnCondition_simple_conditions_").setValue('');
		        			Ext.getCmp("conditions_button_update").setDisabled(true);
		        		}
		        		},{
		        			id:'conditions_button_update',
		        			xtype:'button',
		        			text:'修改查询条件',
		        			disabled:true,
		        			handler:function(){
		        			var left = Ext.getCmp('selColumnConditionMethod_simple_conditions_').getValue();
		        			if(!Ext.getCmp('selColumnCondition_simple_conditions_').validate()){
		        				return;
		        			}
		        			var right = Ext.getCmp('selColumnCondition_simple_conditions_').getValue();
		        			var record=Ext.getCmp('conditions_grid_').getSelectionModel().getSelected();
		        			if(!record){
		        				alert("需要修改的条件不存在,可能已经被删除");
		        			}else{
		        				record.data.join = left;
		        				record.data.code = right;
		        				record.commit();
		        			}
		        			Ext.getCmp("selColumnConditionMethod_simple_conditions_").setValue('and');
		        			Ext.getCmp("selColumnCondition_simple_conditions_").setValue('');
		        			Ext.getCmp("conditions_button_update").setDisabled(true);
		        			Ext.getCmp("conditions_button_add").setDisabled(false);
		        		}
		        		},					{
		        			layout: 'table',
		        			border:false,
		        			height: 450,
		        			bodyStyle:'padding:10px 10px 10px 20px',
		        			layoutConfig: {columns:5},
		        			defaults:{frame:false,style:'padding:2px',minWidth: '60',style:{marginBottom: '10px'}},
		        			items:[
		        			       {
		        			    	   xtype: "button",
		        			    	   text: "substr",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("substr()");
		        			    	   }
		        			       }
		        			       , {
		        			    	   xtype: "button",
		        			    	   text: "trim",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("trim()");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "replace",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("replace()");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " <b>Clean</b> ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   Ext.getCmp("selColumnCondition_simple_conditions_").setValue('');
		        			    	   }
		        			       }
		        			       , {
		        			    	   xtype: "button",
		        			    	   text: " + ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" + ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "nvl",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("nvl()");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "round",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("round()");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "decode",
		        			    	   handler: function(){
		        			    	   writeCondition("decode()");
		        			       }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "'",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition("'");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " - ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" - ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "is not null",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" is not null ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "is null",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" is null ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "not in",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" not in() ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: "like",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" like '%%' ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " * ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" * ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " > ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" > ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " < ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" < ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " = ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" = ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " ( ) ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" (  ) ");
		        			    	   }
		        			       }
		        			       ,{
		        			    	   xtype: "button",
		        			    	   text: " / ",
		        			    	   style:
		        			    	   {
		        			    	   marginBottom: '5px'
		        			    	   },
		        			    	   handler: function(){
		        			    		   writeCondition(" / ");
		        			    	   }
		        			       }
		        			       ]
		        		}]
		        	}]
		        },Ext.getCmp('conditions_grid_')
		        ]
		});
	}
	Ext.getCmp('customH_win_conditions_').show();
}